package com.example.models;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

import org.springframework.boot.orm.jpa.EntityScan;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;

@Entity
public class Cart {
	@Id
	@GeneratedValue //COMMENT THIS WHEN USING MONGO, AND MAKE IT A STRING
	private String id;  //mongo
//	private Long id; //mysql
	
	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	private Set<ItemToPurchase> items= new HashSet<>();
	
	@ManyToOne
	private Customer customer;
	
	public Cart(){ //jpa only
		
	}
//	
//	public Cart(Set<ItemToPurchase> items, Customer customer){
//		this.items = items;
//		this.customer = customer;
//	}
	
	@JsonCreator
	public Cart(@JsonProperty("items") Set<ItemToPurchase> items,
			@JsonProperty("customer") Customer customer){
		this.items = items;
		this.customer = customer;
	}
	
	public Cart(String id, Set<ItemToPurchase> items, Customer customer) { //mongo
//	public Cart(Long id, Set<ItemToPurchase> items, Customer customer) {  //mysql
		super();
		this.id = id;
		this.items = items;
		this.customer = customer;
	}


	public Cart(Customer customer){
		this.customer=customer;
	}
	
//	public Cart(Long id) {
	public Cart(String id) {
		this.id=id;
		// TODO Auto-generated constructor stub
	}

	public void addItem(ItemToPurchase item){
		this.items.add(item);
	}
	
	@Override
	public String toString(){
		String result="Id: "+id;
		result+=". Items: ";
		for(ItemToPurchase item: this.getItems()){
			result+=item.toString();
		}
		return result;
	}

	
	

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
//
//	public Long getId() {
//		return id;
//	}
//
//	public void setId(Long id) {
//		this.id = id;
//	}

	public Set<ItemToPurchase> getItems() {
		return items;
	}

	public void setItems(Set<ItemToPurchase> items) {
		this.items = items;
	}
	
}
